package pub.tbc.atps.process.executor;

import lombok.extern.slf4j.Slf4j;
import pub.tbc.atps.annotation.HandlerOrder;
import pub.tbc.atps.process.Handler;
import pub.tbc.atps.process.HandlerContext;
import pub.tbc.atps.task.HttpMethod;
import pub.tbc.atps.task.RestTask;
import pub.tbc.atps.template.RequestTemplate;
import pub.tbc.toolkit.Objs;

/**
 * @author tbc on 2016/11/29 13:22:24.
 */
@Slf4j
@HandlerOrder(10)
public class Executor implements Handler {
    @Override
    public boolean handler(HandlerContext context) {
        RestTask task = context.getAttribute("task");
        if (Objs.isEmpty(task)) {
            log.error("");
            return false;
        }

        HttpMethod httpMethod = HttpMethod.getHttpMethod(task.getHttpMethod());
        if (Objs.isEmpty(httpMethod)) {
            log.error("");
            return false;
        }
        // 执行任务
        String jsonStr = RequestTemplate.getInstance().request(task);
        // 记录到任务上下文
        context.setAttribute("task.result", jsonStr);
        log.debug(jsonStr);
        return true;
    }

}
